<?php

/**
 * Created on May 16.
 *  * @author Samy Deghou(deghou@polytech.unice.fr ; deghou@embl.de)
 * 		   Stephan Beisken (beisken@ebi.ac.uk)
 * 		   Luis Felipe De Figueiredo (ldpf@ebi.ac.uk)
 */
class Bond implements iCRUD {
    /*
     * Attributes
     */

    private $_bondID;
 //auto increment in the database
    private $_degree;
    private $_stereo;
    private $_isConfigSpec;
	private $_innerId;
	
	
	public function Bond(){
		$this->_bondID = -1;
        $this->_degree = null;
        $this->_stereo = null;
        $this->_innerId = null;
        $this->_isConfigSpec = null;
	}
	
	
    /*
     * Constructor
     */
    public function __Bond($innerId, $degree, $stereo, $isConfigSpec, $bondID = -1) {
        $this->_bondID = $bondID;
        $this->_degree = $degree;
        $this->_stereo = $stereo;
        $this->_innerId = $innerId;
        $this->_isConfigSpec = $isConfigSpec;
    }

    /**
     * Accessors
     */
    public function get_bondID() {
        return $this->_bondID;
    }

    public function get_degree() {
        return $this->_degree;
    }

    public function get_stereo() {
        return $this->_stereo;
    }

    public function get_isConfigSpec() {
        return $this->_isConfigSpec;
    }

    /**
     * Modifiers
     */
    public function set_bondID($_bondID) {
        $this->_bondID = $_bondID;
    }

    public function set_degree($_degree) {
        $this->_degree = $_degree;
    }

    public function set_stereo($_stereo) {
        $this->_stereo = $_stereo;
    }

    public function set_isConfigSpec($_isConfigSpec) {
        $this->_isConfigSpec = $_isConfigSpec;
    }

    /*
     * CRUD
     */

    /**
     * Put an object in the database
     */
    public function create() {

        $connetion=Database::connect();
        if ($this->_bondID != -1)
            die("Unable to create, still existing!");
        else {
            $query = "INSERT INTO `BOND` values (NULL,'" .
                    $this->_bondID . "','" .
                    $this->_degree . "','" .
                    $this->_stereo . "','" .
                    $this->_isConfigSpec . "')";
            mysql_query($query) or die(mysql_error());
            $this->_idSujet = mysql_insert_id();
        }
		Database::disconnect($connection);
        return $this->_idSujet;
    }
    
    
    public static function createAndReturn($arrayOfBonds){
    	$mapOfBondsAtom=array();
    	$connetion=Database::connect();
    	foreach($arrayOfBonds as $bond){
    		if ($bond->_bondID != -1)
        	    die("Unable to create, still existing!");
       		 else {
            	$query = "INSERT INTO `BOND` values (NULL,'".
                    $bond->_degree . "','" .
                    $bond->_stereo . "','" .
                    $bond->_isConfigSpec . "')"; 
//                 echo("THE QUERY:  ".$query."</br></br>");                
           		 mysql_query($query) or die(mysql_error());
           		 $bond->_bondID = mysql_insert_id();
           		 $mapOfBondsAtom[$bond->_innerId]=$bond->_bondID;
	        } 
    	}
    	Database::disconnect($connection);
    	return $mapOfBondsAtom;
    }
    

    /**
     * Read an object from the database
     */
    public static function read($key) {

        $connetion=Database::connect();
        $query = "SELECT * FROM BOND WHERE bond_id = '" . $key . "'";
        $ans = mysql_query($query);
        $res = mysql_fetch_assoc($ans);
        extract($res);
        $bond=new Bond();
        $bond->__Bond(null,$degree, $stereo, $is_config_spec,$bond_id);
		Database::disconnect($connection);
        return $bond;
    }

    /*
     * Update an object from the database
     */

    public function update() {
        $connection=Database::connect();
        $query = "UPDATE `BOND` SET degree='" .
                $this->_degree . "', stereo='" .
                $this->_stereo . "', is_config_spec='" .
                $this->_isConfigSpec . "' WHERE bond_id='" .
                $this->_bondID . "'";
        mysql_query($query) or die(mysql_error());
        Database::disconnect($connection);
    }

    /*
     * Delete an object from the database
     */

    public function delete() {

        $connetion=Database::connect();
        $query = "DELETE FROM `BOND` WHERE bond_id='" . $this->_bondID . "'";
        mysql_query($query) or die(mysql_error());
        Database::disconnect($connection);
    }
    
    

    
    
    
    
    

}
?>
